package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;



/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {
    @Autowired
    private ProductUnitService productUnitService;
    @Operation(summary = "查询商品单位列表")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(description = "商品单位名称") @RequestParam(required = false) String name){
        //开启分页查询
        startPage();
        List<ProductUnit> list = productUnitService.selectProdectList(name);
        return getDataTable(list);
    }
    @Operation(summary = "获取商品详细信息")
    @GetMapping("/{id}")
    public AjaxResult getProductUnitById(@Parameter(description = "商品单位id") @PathVariable Long id){
        ProductUnit productUnit = productUnitService.getById(id);
        return AjaxResult.success(productUnit);
    }
    @Operation(summary = "修改商品单位")
    @PutMapping
    public AjaxResult updateProductUnit(@Parameter(description = "商品单位") @RequestBody ProductUnit productUnit){
        boolean result = productUnitService.updateById(productUnit);
        return result ? AjaxResult.success() : AjaxResult.error();
    }
    @Operation(summary = "添加商品单位")
    @PostMapping
    public AjaxResult addProductUnit(@Parameter(description = "商品单位") @RequestBody ProductUnit productUnit){
        boolean save = productUnitService.save(productUnit);
        return save ? AjaxResult.success() : AjaxResult.error();
    }
    @Operation(summary = "删除商品单位")
    @DeleteMapping("/{id}")
    public AjaxResult deleteProductUnit(@Parameter(description = "商品单位id") @PathVariable Long id){
        boolean deleted = productUnitService.removeById(id);
        return deleted ? AjaxResult.success() : AjaxResult.error();
    }
    @GetMapping("getUnitAll")
    public AjaxResult getUnitAll(){
        List<ProductUnit> productUnits = productUnitService.list();
        return AjaxResult.success(productUnits);
    }
}
